﻿using RemotingObject;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Imaging;

namespace EnvCommonUtility
{
    /// <summary>
    /// 远程监控操作【服务器端】
    /// </summary>
    public class MonitorOperation
    {
        #region Private Field

        #region Static
        private static MonitorOperation _instance = null;
        private static readonly object _padlock = new Object();
        #endregion

        #endregion

        #region 构造函数

        /// <summary>
        /// 私有构造函数
        /// </summary>
        private MonitorOperation()
        {
            try
            {
                ChannelServices.RegisterChannel(new TcpChannel(), false); // 只能注册一次该默认 tcp 信道
            }
            catch (System.Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }

        #endregion

        #region 属性

        /// <summary>
        /// 单例实体
        /// </summary>
        public static MonitorOperation Instance
        {
            get
            {
                lock (_padlock)
                {
                    if (_instance == null)
                    {
                        _instance = new MonitorOperation();
                    }
                    return _instance;
                }
            }
        }

        #endregion

        #region 公开的方法

        /// <summary>
        /// 获取远程主机代理对象
        /// </summary>
        /// <param name="remoteMachine">远程主机地址：IP | 机器名</param>
        /// <returns></returns>
        public RemotingMonitor GetObject(string remoteMachine)
        {
            try
            {
                return (RemotingMonitor)Activator.GetObject(typeof(RemotingMonitor),
                    string.Format(@"tcp://{0}:9452/MonitorServerUrl", remoteMachine));
            }
            catch (System.Exception ex)
            {
#if DEBUG
                Debug.WriteLine(ex.Message);
#endif
                return null;
            }
        }

        /// <summary>
        /// 更新显示
        /// </summary>
        /// <param name="monitor"></param>
        /// <returns></returns>
        public BitmapSource UpdateDisplay(RemotingMonitor monitor)
        {
            if (monitor == null)
                return null;

            System.Threading.Monitor.Enter(this);
            BitmapSource newBitmapImage = null;

            try
            {
                byte[] BitmapBytes = monitor.GetDesktopBitmapBytes();

                if (BitmapBytes.Length > 0)
                {
                    MemoryStream stream = new MemoryStream(BitmapBytes, false);
                    /* BitmapImage 资源大；BitmapFrame 质量低，用于转解码
                    stream.Seek(0, System.IO.SeekOrigin.Begin);
                    newBitmapImage = new BitmapImage();
                    newBitmapImage.BeginInit();
                    newBitmapImage.StreamSource = stream;
                    newBitmapImage.EndInit();
                    newBitmapImage.Freeze();*/
                    ImageSourceConverter imageSourceConverter = new ImageSourceConverter();
                    newBitmapImage = imageSourceConverter.ConvertFrom(stream) as BitmapFrame;
                }
            }
            catch (System.Exception ex)
            {
#if DEBUG
                Debug.WriteLine(ex.Message);
#endif
            }

            System.Threading.Monitor.Exit(this);
            return newBitmapImage;
        }

        /// <summary>
        /// 心跳检测
        /// </summary>
        /// <param name="monitor"></param>
        /// <returns></returns>
        public bool Heartbeat(RemotingMonitor monitor)
        {
            if (monitor == null)
                return false;

            try
            {
                return monitor.Heartbeat();
            }
            catch (System.Exception ex)
            {
#if DEBUG
                Debug.WriteLine(ex.Message);
#endif
                return false;
            }
        }

        /// <summary>
        /// 关机
        /// </summary>
        /// <param name="monitor"></param>
        public void ShutDownPC(RemotingMonitor monitor)
        {
            if (monitor == null)
                return;

            try
            {
                monitor.ShutDownPC();
            }
            catch (System.Exception ex)
            {
#if DEBUG
                Debug.WriteLine(ex.Message);
#endif
            }
        }

        /// <summary>
        /// 锁屏
        /// </summary>
        /// <param name="monitor"></param>
        public void LockPC(RemotingMonitor monitor)
        {
            if (monitor == null)
                return;

            try
            {
                monitor.LockPC();
            }
            catch (System.Exception ex)
            {
#if DEBUG
                Debug.WriteLine(ex.Message);
#endif
            }
        }

        /// <summary>
        /// 解屏
        /// </summary>
        /// <param name="monitor"></param>
        public void UnlockPC(RemotingMonitor monitor)
        {
            if (monitor == null)
                return;

            try
            {
                monitor.UnlockPC();
            }
            catch (System.Exception ex)
            {
#if DEBUG
                Debug.WriteLine(ex.Message);
#endif
            }
        }

        /// <summary>
        /// 发送消息
        /// </summary>
        /// <param name="monitor"></param>
        /// <param name="msg">内容</param>
        public void SendMessage(RemotingMonitor monitor,
            string msg)
        {
            if (monitor == null)
                return;

            try
            {
                if (string.IsNullOrWhiteSpace(msg))
                {
                    return;
                }

                monitor.SendMessage(msg);
            }
            catch (System.Exception ex)
            {
#if DEBUG
                Debug.WriteLine(ex.Message);
#endif
            }
        }

        #endregion
    }
}
